import re
import logging

from google.appengine.ext import db

BUCKETS = [
  'i love',
  'i hate',
  'i want',
  'i think',
  'i believe',
  'i feel',
  'i wish',
  'i hope'
]

PATTERN = re.compile(r'\b(%s)\b' % '|'.join(BUCKETS), re.IGNORECASE)

class Mood(db.Model):
  name = db.StringProperty(required=True)
  last_summary = db.TextProperty(required=False)
  last_content = db.TextProperty(required=False)
  last_modified = db.DateTimeProperty(auto_now=True)
  counter = db.IntegerProperty(default=0)

def assign(summary, content):
  lower = content.lower()
  for bucket in BUCKETS:
    if lower.find(bucket) != -1:
      logging.info('Entry assigned to \'%s\' bucket.' % bucket)
      query = Mood.all()
      query.filter('name =', bucket)
      mood = query.fetch(1)
      if not mood:
        mood = Mood(name=bucket)
      elif type(mood) == list:
        mood = mood[0]
      else:
        raise TypeError('Expected a list.')
      mood.counter += 1
      mood.last_summary = summary
      mood.last_content = content
      mood.put()
